@extends('Admin.layout.layout')
@section('content')
<div class="row">
    <div class="col-lg-12">
        <h1 class="page-header">分类列表</h1>
    </div>
    <!-- /.col-lg-12 -->
</div>
<div class="row">

<div class="col-lg-12">
@if(session('success'))
	<div class="alert alert-success">
	{{session('success')}}	
	</div>
@endif	
<div id="messagesuccess" class="alert alert-info" style="display:none">
    <span></span> 
</div>
<div id="messagedanger" class="alert alert-danger" style="display:none">
    <span></span>
</div>
    <div class="panel panel-default">
    <div class="panel-heading">
            DataTables Advanced Tables
        </div>
        <!-- /.panel-heading -->
        <div class="panel-body">
            <div class="dataTable_wrapper">
                <div id="dataTables-example_wrapper" class="dataTables_wrapper form-inline dt-bootstrap no-footer">
                <div class="row">
                <div class="col-sm-6">
                <form action="{{url('/admin/cate/index')}}" method="get">
                <div class="dataTables_length" id="dataTables-example_length">
                <label>显示 <select name="num" aria-controls="dataTables-example" class="form-control input-sm">
                <option value="10"@if(!empty($request['num']) && $request['num'] == 10) { selected } @endif>10</option>
                <option value="25"@if(!empty($request['num']) && $request['num'] == 25) { selected } @endif>25</option>
                <option value="50"@if(!empty($request['num']) && $request['num'] == 50) { selected } @endif>50</option>
                <option value="100"@if(!empty($request['num']) && $request['num'] == 100) { selected } @endif>100</option>
                </select> 条</label></div></div>
                <div class="col-sm-6">
                <div id="dataTables-example_filter" class="dataTables_filter">
                <label>分类名:<input name="cname" type="text" class="form-control input-sm" placeholder="" aria-controls="dataTables-example" value="@if(!empty($request['cname'])){{$request['cname']}}@endif">
				<button class="btn btn-primary">搜索</button>
                </label>
                </form>
                </div></div></div>
                <div class="row">
                <div class="col-sm-12">
                <table class="table table-striped table-bordered table-hover dataTable no-footer" id="dataTables-example" role="grid" aria-describedby="dataTables-example_info">
                    <thead>
                        <tr role="row"><th class="sorting_asc" tabindex="0" aria-controls="dataTables-example" rowspan="1" colspan="1" aria-sort="ascending" aria-label="Rendering engine: activate to sort column descending" style="width: 172px;">编号</th>
                        <th class="sorting" tabindex="0" aria-controls="dataTables-example" rowspan="1" colspan="1" aria-label="Browser: activate to sort column ascending" style="width: 204px;">分类名</th>
                        <th class="sorting" tabindex="0" aria-controls="dataTables-example" rowspan="1" colspan="1" aria-label="Platform(s): activate to sort column ascending" style="width: 185px;">父级分类名</th>
                        <th class="sorting" tabindex="0" aria-controls="dataTables-example" rowspan="1" colspan="1" aria-label="CSS grade: activate to sort column ascending" style="width: 110px;">操作</th></tr>
                    </thead>
                    <tbody>                  
                    @foreach($cate as $k=>$v)
                    <tr class="gradeA odd" role="row">
                            <td class="cid">{{$v->id}}</td>
                            <td>{{$v->name}}</td>
                            <td>{{$v->path}}</td>
                            <td class="center"><button class=" fa fa-trash-o btn-danger delete">删除</button>|<a href="{{url('admin/cate/edit',['id'=>$v->id])}}"><button class="fa fa-gear btn-info">修改</button></a></td>
                      </tr>
                    @endforeach  
                        </tbody>
                </table>
                <div class="col-sm-12 col-sm-offset-9">
                        {!!$cate->appends($request)->render()!!}
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@section('myjs')

<script type="text/javascript">
$.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
});
	$('.delete').click(function(){
		var id = $(this).parents('tr').find('.cid').html();
		var dd = $(this);

		$.post('/admin/cate/delete',{id:id},function(data){
			if(data == 1){
				dd.parents('tr').remove();
				message('success','分类删除成功');
			}else if(data == 2){
				message('error','分类下有子类不能删除');
			}else{
				message('error','删除失败');
			}
		})
		return false;
	});
	    //封装函数显示信息
    function message(er,m)
    {
      if(er == 'success'){
        $('#messagesuccess').find('span').html(m);
        $('#messagesuccess').show();
        setTimeout("$('#messagesuccess').hide()",5000);
      }else{
        $('#messagedanger').find('span').html(m);
        $('#messagedanger').show();
        setTimeout("$('#messagedanger').hide()",5000);

      }
    }
</script>
@endsection